package com.lm;

import mapgenerator.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JToggleButton;

/**
 *
 * @author Sajarwo Anggai
 */
public class WorkSpace implements ActionListener, MouseListener, MouseMotionListener {

    private Main comp;
    private int tileX, tileY, mapX, mapY;

    public WorkSpace(Main comp) {
        this.comp = comp;
        comp.ToolbarP.addActionListener(this);
        comp.ToolbarS.addActionListener(this);
        comp.ToolbarRP.addActionListener(this);
        comp.ToolbarRS.addActionListener(this);
    }

    public static int pixelsToTiles(int pixels, int tileSize) {
        return (int) Math.floor((float) pixels / tileSize);
    }

    public static int tilesToPixels(int numTiles, int tileSize) {
        return numTiles * tileSize;
    }
    private JToggleButton currentToolbarButton;

    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        currentToolbarButton = (JToggleButton) ob;
        if (currentToolbarButton.isSelected()) {
            comp.ToolbarP.setSelected(false);
            comp.ToolbarS.setSelected(false);
            comp.ToolbarRP.setSelected(false);
            comp.ToolbarRS.setSelected(false);
            currentToolbarButton.setSelected(true);
        }
    }

    public void drawTile() {
        try {
            if (comp.ToolbarP.isSelected()) {
                patternOperation(true);
            } else if (comp.ToolbarRP.isSelected()) {
                patternOperation(false);
            }
        } catch (Exception e) {
        }
    }

    public void patternOperation(boolean draw) {
        int row = comp.lmControlPanel.getTable().getSelectedRow();
        if (row != -1) {
            Layer layer = (Layer) comp.layerContainer.getLayers().get(row);
            if (layer.isShow()) {
                if (draw) {
                    Tile tile = comp.ipControlPanel.getCurrentTile();
                    if (tile != null) {
                        layer.getTileContaner()[tileX][tileY] = tile;
                    }
                } else {
                    layer.getTileContaner()[tileX][tileY] = null;
                }
                comp.lmControlPanel.gridWorkspace.setDraw(true);
                comp.lmControlPanel.gridWorkspace.drawLayer(tileX, tileY);
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
        drawTile();
        System.out.println("klil");

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
        drawTile();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
    }

    public void setViewTile(int x, int y) {
        comp.InfoMapX.setText("x:" + x);
        comp.InfoMapY.setText("y:" + y);
        mapX = x;
        mapY = y;

        x = Math.max(pixelsToTiles(x, comp.project.getTileWidth()), 0);
        y = Math.max(pixelsToTiles(y, comp.project.getTileHeight()), 0);
        comp.InfoTileX.setText("x:" + x);
        comp.InfoTileY.setText("y:" + y);
        tileX = x;
        tileY = y;
    }

    public int getMapX() {
        return mapX;
    }

    public int getMapY() {
        return mapY;
    }

    public int getTileX() {
        return tileX;
    }

    public int getTileY() {
        return tileY;
    }
}
